package com.luyou.taco;

import lombok.Data;
import org.hibernate.validator.constraints.CreditCardNumber;

import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author weiqi
 * @version 1.0
 * Create by 2024/8/20 20:09
 */
@Data
@Entity
@Table(name="Taco_Order")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    private Date placedAt;

    @NotBlank(message="姓名为必填项")
    private String name;

    @NotBlank(message="街道是必填项")
    private String street;

    @NotBlank(message="城市是必填项")
    private String city;

    @NotBlank(message="州是必填项")
    private String state;

    @NotBlank(message="邮政编码是必需的")
    private String zip;

    @CreditCardNumber(message="不是有效的信用卡号")
    private String ccNumber;

    @Pattern(regexp="^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$", message="必须格式化为MM/YY")
    private String ccExpiration;

    @Digits(integer=3, fraction=0, message="CVV无效")
    private String ccCVV;

    @ManyToMany(targetEntity=Taco.class)
    private List<Taco> tacos = new ArrayList<>();

    public void addDesign(Taco design) {
        this.tacos.add(design);
    }

    @PrePersist
    void placedAt() {
        this.placedAt = new Date();
    }
}
